﻿using System;
using YerongapillyGreens.Web.Extensions;
using YerongapillyGreens.Web.Model;

namespace YerongapillyGreens.Web.Pages
{
    public partial class Contact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnSubmit.Click += new EventHandler(btnSubmit_Click);

            msgSentDiv.Visible = false;
            msgErrorDiv.Visible = false;
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            msgSentDiv.Visible = false;
            msgErrorDiv.Visible = false;

            EmailModel emailModel = new EmailModel();

            if (chkRent.Checked || (!chkRent.Checked && !chkSales.Checked))
                emailModel.ToAddress.Add(Config.MailToRental);

            if (chkSales.Checked || (!chkRent.Checked && !chkSales.Checked))
                emailModel.ToAddress.Add(Config.MailToSales);

            emailModel.RentalEnquiry = chkRent.Checked;
            emailModel.SalesEnquiry = chkSales.Checked;
            emailModel.Name = txtName.Value;
            emailModel.EmailAddress = txtEmail.Value;
            emailModel.Phone = txtPhone.Value;
            emailModel.Subject = "Enquiry from Website";
            emailModel.Message = txtMessage.Value;

            try
            {
                Emailer em = new Emailer();
                em.SendEmail(emailModel);
                msgSentDiv.Visible = true;
                ClearForm();
            }
            catch (Exception)
            {
                msgErrorDiv.Visible = true;
            }
        }

        private void ClearForm()
        {
            txtEmail.Value = "";
            txtMessage.Value = "";
            txtName.Value = "";
            txtPhone.Value = "";
            chkSales.Checked = false;
            chkRent.Checked = false;
        }
    }
}